package cn.clq.org.controller;

import cn.clq.basic.util.AjaxResult;
import cn.clq.basic.util.PageList;
import cn.clq.org.domain.Employee;
import cn.clq.org.query.EmployeeQuery;
import cn.clq.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author clqlongbao
 * @version 1.0
 * @description: 员工表的接口
 * @date 2022/6/5 9:49
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    /**
     * @description:查询单个员工
     * @param: id
     * @return: cn.clq.org.domain.Employee
     * @author clqlongbao
     * @date: 2022/6/5 9:53
     */
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }
    /**
     * @description:查询全部员工
     * @param:
     * @return: java.util.List<cn.clq.org.domain.Employee>
     * @author clqlongbao
     * @date: 2022/6/5 10:52
     */
    @GetMapping
    public List<Employee> queryAll(){
        return employeeService.queryAll();
    }
    /**
     * @description:高级分页查询
     * @param: employeeQuery
     * @return: cn.clq.basic.util.PageList<cn.clq.org.domain.Employee>
     * @author clqlongbao
     * @date: 2022/6/5 10:55
     */
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }
    /**
     * @description:根据id删除一个员工
     * @param: id
     * @return: cn.clq.basic.util.AjaxResult
     * @author clqlongbao
     * @date: 2022/6/5 11:01
     */
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            employeeService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统瓦了");
        }
    }
    /**
     * @description:判断是新增还是修改的接口
     * @param: employee
     * @return: cn.clq.basic.util.AjaxResult
     * @author clqlongbao
     * @date: 2022/6/5 11:05
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                //新增
                employeeService.save(employee);
            }else{
                //修改
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统瓦了");
        }
    }

    /**
     * @description:批量删除
     * @param: ids
     * @return: cn.clq.basic.util.AjaxResult
     * @author clqlongbao
     * @date: 2022/6/6 18:22
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody Long[] ids){
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试！！！");
        }
    }
}
